package com.destination.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.destination.model.ConnUtil;

public class ShowLessonSers extends Activity {


	private ConnUtil cu=new ConnUtil();
	private Dialog dlg=null;
	private String strResult=null;
	private ListView lv_lessonSers=null;
	private lessonSersAsync async=null;
	private List<Map<String, String>> lessonSers=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_lessonsers);
		
		lv_lessonSers=(ListView)findViewById(R.id.lv_lessonsers);
		
		lv_lessonSers.setOnItemClickListener(new lvLessonSerListener());
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		async=new lessonSersAsync();
		async.execute(100);
		super.onResume();
	}
	private class lvLessonSerListener implements OnItemClickListener{
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			String str=cu.getDisLessonInfo(String.valueOf(lessonSers.get(arg2).get("lessonSer_id")));
			String title=lessonSers.get(arg2).get("lessonSer_time")+"    "+
							lessonSers.get(arg2).get("lessonSer_name")+"   "+
							lessonSers.get(arg2).get("lessonSer_dept");
			String content=new String();
			try {
				List<Map<String ,String>> lessons=new ArrayList<Map<String,String>>();
				lessons=parseLesssonsMapJson(str);
				int j=lessons.size();
				for(int i=0;i<j;i++)
					content+="\t"+(i+1)+")  "+lessons.get(0).get("lesson_time")+"   "+lessons.get(i).get("lesson_name")+"\n";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			LayoutInflater inflater=LayoutInflater.from(ShowLessonSers.this);
			View myView=inflater.inflate(R.layout.lessons_show_dlg, null);
			TextView txt_title=(TextView)myView.findViewById(R.id.txt_lessons_title);
			TextView txt_content=(TextView)myView.findViewById(R.id.txt_lessons_content);
			txt_title.setText(title);
			txt_content.setText(content);
			Dialog dg=new AlertDialog.Builder(ShowLessonSers.this)
					.setView(myView)
					.setNegativeButton("确定", new DialogInterface.OnClickListener() {							
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dlg.dismiss();
						}
					})
					.create();
			dg.show();
					
		}
	}
	
	private class lessonSersAsync extends AsyncTask<Integer, Integer, String>{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			dlg=new AlertDialog.Builder(ShowLessonSers.this)
				.setMessage("正在加载中……")
				.create();
			dlg.show();
			super.onPreExecute();
		}
		@Override
		protected String doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			strResult=cu.getDisLessonSersInfo();
			return strResult;
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub	
			lessonSers=new ArrayList<Map<String,String>>();
			try {
				lessonSers=parseLesssonSersMapJson(result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			SimpleAdapter simple=new SimpleAdapter(ShowLessonSers.this, 
					lessonSers, R.layout.lessonsers_item, 
					new String[]{"lessonSer_id","lessonSer_name","lessonSer_dept","lessonSer_time"},
					new int[]{R.id.txt_lessonSer_id,R.id.txt_lessonSer_name,R.id.txt_lessonSer_dept,R.id.txt_lessonSer_time});
			lv_lessonSers.setAdapter(simple);
			dlg.dismiss();		
			super.onPostExecute(result);
		}
	}
	
	public List<Map<String,String>> parseLesssonSersMapJson(String dataJson) throws Exception{
		
		List<Map<String,String>> lessonSers=new ArrayList<Map<String,String>>();
		JSONArray jsonArr=new JSONArray(dataJson.toString().trim());
		for(int i=0;i<jsonArr.length();i++){		
			Map<String,String> lessonSer=new HashMap<String, String>();
			JSONObject jsonObject=jsonArr.getJSONObject(i);
			lessonSer.put("lessonSer_id",String.valueOf(jsonObject.getInt("lessonSer_id")));
			lessonSer.put("lessonSer_name",jsonObject.getString("lessonSer_name"));
			lessonSer.put("lessonSer_dept",jsonObject.getString("lessonSer_dept"));
			lessonSer.put("lessonSer_lessonIDs", jsonObject.getString("lessonSer_lessonIDs"));
			lessonSer.put("lessonSer_time", 
					jsonObject.getString("lessonSer_begtime").substring(0,jsonObject.getString("lessonSer_begtime").lastIndexOf(":"))+"-"+
					jsonObject.getString("lessonSer_endtime").substring(0,jsonObject.getString("lessonSer_endtime").lastIndexOf(":")));
			lessonSers.add(lessonSer);
		}
		return lessonSers;
	}
	
	public List<Map<String,String>> parseLesssonsMapJson(String dataJson) throws Exception{
		
		List<Map<String,String>> lessons=new ArrayList<Map<String,String>>();
		JSONArray jsonArr=new JSONArray(dataJson.toString().trim());
		for(int i=0;i<jsonArr.length();i++){		
			Map<String,String> lesson=new HashMap<String, String>();
			JSONObject jsonObject=jsonArr.getJSONObject(i);
			lesson.put("lesson_id",String.valueOf(jsonObject.getInt("lesson_id")));
			lesson.put("lesson_name",jsonObject.getString("lesson_name"));
			lesson.put("lesson_time", 
					jsonObject.getString("lesson_begtime").substring(0,jsonObject.getString("lesson_begtime").lastIndexOf(":"))+"-"+
					jsonObject.getString("lesson_endtime").substring(0,jsonObject.getString("lesson_endtime").lastIndexOf(":")));
			lessons.add(lesson);
		}
		return lessons;
	}
}
